package hot100;

public class Solution122 {
    public int maxProfit(int[] prices) {
        int last0 = -prices[0];
        int last1 = 0;
        int maxProfit = 0;
        for (int i = 0; i < prices.length; i++) {
            int current0 = Math.max(last0, last1 - prices[i]);
            int current1 = Math.max(last1, last0 + prices[i]);
            maxProfit = Math.max(current0, current1);
            last0 = current0;
            last1 = current1;
        }
        return maxProfit;
    }
}
